package com.wf.day_0607.test5;

import java.io.*;
import java.util.Arrays;

public class Demo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Student student = new Student("小王", 20, new Phone("iPhone 15 pro max", 8888.88));

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(baos)) {

            oos.writeObject(student);
            oos.flush();

            // 获取序列化后的字节数组
            byte[] byteArray = baos.toByteArray();
            System.out.println(Arrays.toString(byteArray));

            try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
                 ObjectInputStream ois = new ObjectInputStream(bais)) {

                // 反序列化对象
                Student clonedStudent = (Student) ois.readObject();
                System.out.println("深克隆对象：" + clonedStudent);
            }
        }
    }
}
